from itertools import accumulate
from typing import List


class Solution:
    def stoneGameVII(self, stones: List[int]) -> int:
        n = len(stones)
        s = list(accumulate(stones, initial=0))
        f = [0]*n
        for i in range(n-2, -1, -1):
            for j in range(i+1 ,n):
                f[j] = max(s[j]-s[i]-f[j-1], s[j+1]-s[i+1]-f[j])
        return f[-1]